﻿using OnlineStore.Common;
using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace OnlineStore.View
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            viewModel = new MianViewModel();
            this.DataContext = viewModel;
        }

        MianViewModel viewModel;
        

        /// <summary>
        /// 鼠标左键拖动事件，通过鼠标左键可以拖动窗口
        /// </summary>
        private void WinMove_LeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(delegate
            {
                if (e.LeftButton == MouseButtonState.Pressed)
                {
                    this.DragMove();
                }
            }));
        }

        /// <summary>
        /// 关闭主窗口事件
        /// </summary>
        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(delegate
            {
                this.Close();

                if (viewModel.MyLogWindow != null)
                {
                    viewModel.MyLogWindow.Close();
                }
                
                if (viewModel.MyPlaywright != null)
                {
                    viewModel.MyPlaywright.Dispose();
                }
            }));
        }
        
        /// <summary>
        /// 主窗口缩小到任务栏事件
        /// </summary>
        private void MinButton_Click(object sender, RoutedEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(delegate
            {
                this.WindowState = WindowState.Minimized;
            }));
        }

        /// <summary>
        /// 文本框数据变更事件，自动下拉到最底部
        /// </summary>
        private void TextBox_textChanged(object sender, TextChangedEventArgs e)
        {
            this.Dispatcher.Invoke(new Action(delegate
            {
                ScrollViewer.ScrollToEnd();
            }));
        }
    }
}
